package cn.gailvlun.gll.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import cn.gailvlun.gll.Constants;
import cn.gailvlun.gll.net.R;
import cn.gailvlun.gll.util.DensityUtil;
import io.reactivex.functions.Consumer;




public class NavigationBarView extends LinearLayout implements View.OnClickListener {

    public enum MenuType {
        NONE, ICON, TEXT, TWO_ICON;

        public static MenuType valueOf(int ordinal) {
            for (MenuType menuType :
                    values()) {
                if (menuType.ordinal() == ordinal) {
                    return menuType;
                }
            }
            return NONE;
        }
    }

    public NavigationBarView(Context context) {
        this(context, null);
    }

    public NavigationBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(attrs);
    }

    private int bgColor;
    private boolean showBack;
    private int backIcon;
    private String titleText;
    private int titleColor;
    private int titleSize;
    private MenuType mMenuType;
    private int menuIcon;
    private int menuLeftIcon;
    private String menuText;
    private ColorStateList menuColor;
    private int menuBG;
    private int menuSize;
    private boolean menuEnable;
    private int menuTextMarginRight;
    private Drawable menuBg;

    private RelativeLayout rl;
    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMenu;
    private TextView tvMenu;
    private ImageView ivLeftMenu;

    private void init(@Nullable AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.NavigationBarView);
        bgColor = ta.getColor(R.styleable.NavigationBarView_bgColor, getResources().getColor(R.color.transparent));
        showBack = ta.getBoolean(R.styleable.NavigationBarView_showBack, true);
        backIcon = ta.getResourceId(R.styleable.NavigationBarView_backIcon, R.drawable.nav_icon_default);
        titleText = ta.getString(R.styleable.NavigationBarView_titleText);
        titleColor = ta.getColor(R.styleable.NavigationBarView_titleColor, getResources().getColor(R.color.blackTwo));
        titleSize = ta.getDimensionPixelSize(R.styleable.NavigationBarView_titleSize, DensityUtil.dp2px(getContext(), 16));
        mMenuType = MenuType.valueOf(ta.getInt(R.styleable.NavigationBarView_menuType, 0));
        menuIcon = ta.getResourceId(R.styleable.NavigationBarView_menuIcon, R.drawable.nav_icon_set_default);
        menuLeftIcon = ta.getResourceId(R.styleable.NavigationBarView_menuLeftIcon, R.drawable.nav_icon_set_default);
        menuText = ta.getString(R.styleable.NavigationBarView_menuText);
        menuColor = ta.getColorStateList(R.styleable.NavigationBarView_menuColor);
        if (menuColor == null) {
            menuColor = ColorStateList.valueOf(getResources().getColor(R.color.pinkishGrey));
        }
        menuBG = ta.getResourceId(R.styleable.NavigationBarView_menuBG, -1);
        menuSize = ta.getDimensionPixelSize(R.styleable.NavigationBarView_menuSize, DensityUtil.dp2px(getContext(), 13));
        menuEnable = ta.getBoolean(R.styleable.NavigationBarView_menuEnable, true);
        menuTextMarginRight = ta.getDimensionPixelSize(R.styleable.NavigationBarView_menuTextMarginRight, 0);
        ta.recycle();

        View view = View.inflate(getContext(), R.layout.navigation_bar_view, this);
        rl = view.findViewById(R.id.rl);
        ivBack = view.findViewById(R.id.iv_back);
        tvTitle = view.findViewById(R.id.tv_title);
        ivMenu = view.findViewById(R.id.iv_menu);
        tvMenu = view.findViewById(R.id.tv_menu);
        ivLeftMenu = view.findViewById(R.id.iv_left_menu);

        ivBack.setOnClickListener(this);
        RxView.clicks(ivMenu)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mOnMenuClickListener != null) {
                            mOnMenuClickListener.onMenuClick(NavigationBarView.this);
                        }
                        if (mOnMenuClickListener2 != null) {
                            mOnMenuClickListener2.onMenuClick(NavigationBarView.this, 1);
                        }
                    }
                });
        RxView.clicks(ivLeftMenu)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mOnMenuClickListener != null) {
                            mOnMenuClickListener.onMenuClick(NavigationBarView.this);
                        }
                        if (mOnMenuClickListener2 != null) {
                            mOnMenuClickListener2.onMenuClick(NavigationBarView.this, 0);
                        }
                    }
                });
        RxView.clicks(tvMenu)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mOnMenuClickListener != null) {
                            mOnMenuClickListener.onMenuClick(NavigationBarView.this);
                        }
                        if (mOnMenuClickListener2 != null) {
                            mOnMenuClickListener2.onMenuClick(NavigationBarView.this, -1);
                        }
                    }
                });

        rl.setBackgroundColor(bgColor);

        ivBack.setVisibility(showBack ? View.VISIBLE : View.INVISIBLE);
        ivBack.setImageResource(backIcon);
        tvTitle.setText(titleText);
        tvTitle.setTextColor(titleColor);
        tvTitle.setTextSize(DensityUtil.px2dp(getContext(), titleSize));
        tvTitle.setOnClickListener(this);
        if (menuBG != -1) {
            tvMenu.setBackgroundResource(menuBG);
        }

        switch (mMenuType) {
            case NONE: {
                ivMenu.setVisibility(GONE);
                tvMenu.setVisibility(GONE);
                break;
            }
            case ICON: {
                ivMenu.setVisibility(VISIBLE);
                tvMenu.setVisibility(GONE);
                break;
            }
            case TEXT: {
                ivMenu.setVisibility(GONE);
                tvMenu.setVisibility(VISIBLE);
                break;
            }
            case TWO_ICON: {
                ivMenu.setVisibility(VISIBLE);
                ivLeftMenu.setVisibility(VISIBLE);
                tvMenu.setVisibility(GONE);
                break;
            }
        }
        ivMenu.setImageResource(menuIcon);
        ivLeftMenu.setImageResource(menuLeftIcon);
        tvMenu.setText(menuText);
        tvMenu.setTextColor(menuColor);
        tvMenu.setTextSize(DensityUtil.px2dp(getContext(), menuSize));
        ((MarginLayoutParams) tvMenu.getLayoutParams()).rightMargin = menuTextMarginRight;

        ivMenu.setEnabled(menuEnable);
        ivLeftMenu.setEnabled(menuEnable);
        tvMenu.setEnabled(menuEnable);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            if (mOnBackListener != null) {
                mOnBackListener.onBack(this);
            }
        } else if (v.getId() == R.id.tv_title) {
            if (clickDouble()) {
                if (mOnDoubleClickListener != null) {
                    mOnDoubleClickListener.onDoubleClick();
                }
            }
        }
    }


    public interface OnDoubleClickListener {
        void onDoubleClick();
    }

    private OnDoubleClickListener mOnDoubleClickListener;

    public void setOnDoubleClickListener(OnDoubleClickListener doubleClickListener) {
        this.mOnDoubleClickListener = doubleClickListener;
    }

    private Long lastClick = 0L;

    private boolean clickDouble() {
        boolean isDouble = false;
        Long now = System.currentTimeMillis();
        if (now - lastClick < 1000 * Constants.doubleClickDuration) {
            isDouble = true;
        }
        lastClick = now;
        return isDouble;
    }

    public void setBgColor(@ColorInt int bgColor) {
        this.bgColor = bgColor;
        rl.setBackgroundColor(bgColor);
    }






    public void setTitleColor(@ColorRes int titleColorRes) {
        this.titleColor = ContextCompat.getColor(getContext(), titleColorRes);
        tvTitle.setTextColor(titleColor);
    }

    public int getTitleColor() {
        return titleColor;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
        tvTitle.setText(this.titleText);
    }

    public String getTitleText() {
        return titleText;
    }

    public void setMenuIcon(int menuIcon) {
        this.menuIcon = menuIcon;
        ivMenu.setVisibility(VISIBLE);
        tvMenu.setVisibility(GONE);
        ivMenu.setImageResource(menuIcon);
    }

    public int getMenuIcon() {
        return menuIcon;
    }

    public void setBackIcon(int backIcon) {
        this.backIcon = backIcon;
        ivBack.setImageResource(backIcon);
    }

    public int getBackIcon() {
        return backIcon;
    }

    
    public void setMenuTextColor(@ColorRes int menuColor) {
        this.menuColor = ColorStateList.valueOf(getResources().getColor(menuColor));
        tvMenu.setTextColor(this.menuColor);
    }

    public void setMenuTextColor(ColorStateList colorStateList) {
        this.menuColor = colorStateList;
        tvMenu.setTextColor(this.menuColor);
    }

    public void setMenuText(String menuText) {
        this.menuText = menuText;
        tvMenu.setText(menuText);
    }

    public void setMenuVisibility(int visible) {
        ivMenu.setVisibility(visible);
    }

    public void setMenuTextSize(int menuSize) {
        this.menuSize = menuSize;
        tvMenu.setTextSize(DensityUtil.px2dp(getContext(), menuSize));
    }

    

    public void setMenuEnable(boolean isEnable) {
        ivMenu.setEnabled(isEnable);
        tvMenu.setEnabled(isEnable);
    }

    public void setMenuBg(Drawable menuBg) {
        this.menuBg = menuBg;
        tvMenu.setBackground(menuBg);
    }

    @BindingAdapter("menuEnable")
    public static void menuEnable(NavigationBarView nbv, boolean isEnable) {
        if (nbv != null) {
            nbv.setMenuEnable(isEnable);
        }
    }
    @BindingAdapter("menuColor")
    public static void menuColor(NavigationBarView nbv, @ColorInt int menuColorRes) {
        if (nbv != null) {
            nbv.setMenuTextColor(ColorStateList.valueOf(menuColorRes));
        }
    }

    @BindingAdapter("menuBg")
    public static void menuBg(NavigationBarView nbv, Drawable menuBg) {
        if (nbv != null) {
            nbv.setMenuBg(menuBg);
        }
    }

    @BindingAdapter("menuVisibility")
    public static void menuVisibility(NavigationBarView nbv, int visible) {
        nbv.setMenuVisibility(visible);
    }

    public interface OnBackListener {
        void onBack(NavigationBarView nb);
    }

    public interface OnMenuClickListener {
        void onMenuClick(NavigationBarView nb);
    }

    public interface OnMenuClickListener2 {
        void onMenuClick(NavigationBarView nb, int position);
    }

    private OnBackListener mOnBackListener;
    private OnMenuClickListener mOnMenuClickListener;
    private OnMenuClickListener2 mOnMenuClickListener2;

    public void setOnBackListener(OnBackListener onBackListener) {
        this.mOnBackListener = onBackListener;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.mOnMenuClickListener = onMenuClickListener;
    }

    public void setOnMenuClickListener2(OnMenuClickListener2 onMenuClickListener2) {
        this.mOnMenuClickListener2 = onMenuClickListener2;
    }

}
